<template>
  <div class="app-container" >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料子编号" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入物料子编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="itemId">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

 <!--     <el-form-item label="供应商" prop="supplyId">
        <el-input
          v-model="queryParams.supplyId"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
 <!--     <el-form-item label="大类" prop="classId">
        <el-input
          v-model="queryParams.classId"
          placeholder="请输入大类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="物料类别" prop="classId">
        <el-select style="width: 100%" v-model="queryParams.classId" placeholder="物料类别" >
          <el-option
            v-for="dict in classOptions"
            :key="dict.classId"
            :label="dict.className"
            :value="dict.classId"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>

   <!--   <el-form-item label="库存预警" prop="yjflag">
        <el-select style="width:100%" v-model="queryParams.yjflag" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.kc_yj"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>-->

      <!--<el-form-item label="供应商" prop="supplyId">
        <el-select style="width: 100%" v-model="queryParams.supplyId" placeholder="供应商" >
          <el-option
            v-for="dict in supplyOptions"
            :key="dict.id"
            :label="dict.supplyName"
            :value="dict.id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>-->

<!--      <el-form-item label="仓库" prop="stockId">
        <el-input
          v-model="queryParams.stockId"
          placeholder="请输入仓库"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!--<el-form-item label="仓库" prop="stockId">
        <el-select clearable  v-model="queryParams.stockId" placeholder="请选择仓库" clearable>
          <el-option
            v-for="dict in warehouseinfo"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>-->



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:stockinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:stockinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:stockinfo:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:stockinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :row-class-name="tableRowClassName" v-loading="loading" :data="stockinfoList" @selection-change="handleSelectionChange">
<!--
      <el-table-column type="selection" width="55" align="center" />
-->
  <!--    <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="部门id" align="center" prop="deptId" />-->
      <el-table-column label="物料名称" align="center" prop="itemName" />
     <!-- <el-table-column label="物料号" align="center" prop="itemId" width="150px"/>-->
      <el-table-column label="物料编号" show-overflow-tooltip width="140" align="center" prop="itemCode" />
      <el-table-column label="物料子编号" width="150" align="center" prop="itemId">
        <template slot-scope="scope">
          <div style="cursor:pointer;text-decoration:underline;color: blue" class="caigouDanhao" @click="rowClickInfo(scope.row)">{{ scope.row.itemId }}</div>
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="spec" />
    <!--  <el-table-column label="单位" align="center" prop="unit" />-->
      <el-table-column label="单位" align="center" prop="unit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jc_iteminfo_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
    <!--  <el-table-column label="批次号" align="center" prop="lot" />-->
      <el-table-column label="库存数量" align="center" prop="kcNum" />
     <!-- <el-table-column label="锁账数量" align="center" prop="lockNum" />-->
      <el-table-column label="可用数量" align="center" prop="kyNum" >
        <template slot-scope="scope">
          <div>{{scope.row.kcNum - scope.row.lockNum}}</div>
        </template>
      </el-table-column>
    <!--  <el-table-column label="质量状态;" align="center" prop="qutlityStauts" />-->
      <!--<el-table-column label="供应商" align="center" prop="supplyId" />-->
<!--      <el-table-column label="供应商" align="center" prop="supplyName" />-->
      <!--<el-table-column label="大类编码" align="center" prop="classId" />-->
      <el-table-column label="物料类别" align="center" prop="className" />
    <!--  <el-table-column label="仓库编码" align="center" prop="stockId" />-->
<!--
      <el-table-column label="仓库" align="center" prop="stockName" />
-->
<!--
      <el-table-column label="备注" align="center" prop="remark" />
-->
   <!--   <el-table-column label="逻辑乐观锁" align="center" prop="version" />-->
   <!--   <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:stockinfo:edit']"
          >修改</el-button>
     &lt;!&ndash;     <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:stockinfo:remove']"
          >删除</el-button>&ndash;&gt;
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="物料名称" prop="itemName">
          <el-input v-model="form.itemName" readonly="true" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料号" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入物料号" />
        </el-form-item>
        <el-form-item label="库存数量" prop="kcNum">
          <el-input v-model="form.kcNum" placeholder="请输入库存数量" />
        </el-form-item>

        <el-form-item label="供应商" prop="supplyId">
          <el-input v-model="form.supplyId" readonly="true" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="大类" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入大类编码" />
        </el-form-item>
     <!--   <el-form-item label="仓库" prop="stockId">
          <el-input v-model="form.stockId" readonly="true" placeholder="请选择仓库" />
        </el-form-item>-->
     <!--   <el-form-item label="仓库名称" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入仓库名称" />
        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
       <!-- <el-form-item label="逻辑乐观锁" prop="version">
          <el-input v-model="form.version" placeholder="请输入逻辑乐观锁" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--    详情-->
    <el-dialog :close-on-click-modal="false" :title="stockinfo2.title" :visible.sync="stockinfo2.open"  width="1500px" append-to-body>
      <Stockinfo2 ref="stockinfo2"></Stockinfo2>
    </el-dialog>

  </div>
</template>

<script>
import { listStockinfo, getStockinfo, delStockinfo, addStockinfo, updateStockinfo,listinfo } from "@/api/business/stockinfo";
import { listClass} from "@/api/base/class";
import { listSupplier} from "@/api/supplychain/supplier";
import { getListWarehouseinfo } from "@/api/base/warehouseinfo";
import Stockinfo2 from "./index2";
export default {
  components: {
    Stockinfo2
  },
  name: "Stockinfo",
  dicts: ['jc_iteminfo_unit','kc_yj'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存信息表格数据
      stockinfoList: [],

      //详情数据
      stockinfoListInfo: [],
      classOptions: [],
      supplyOptions:[],
      warehouseinfo:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      infoOpen: false,
      infoTitle: "",
      infoLoading:true,
      stockinfo2:{
        // 是否显示弹出层（
        open: false,
        // 弹出层标题
        title: "",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        itemName: null,
        itemId: null,
        itemCode: null,
        lot: null,
        kcNum: null,
        qutlityStauts: null,
        supplyId: null,
        supplyName: null,
        classId: null,
        className: null,
        stockId: null,
        stockName: null,
        version: null,
        yjFlag:null,
        spec:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    listClass().then(response => {
      this.classOptions = response.rows;
    });
    listSupplier().then(response => {
      this.supplyOptions = response.rows;
    });
    getListWarehouseinfo().then(response => {
      this.warehouseinfo = response.data;
    });
  },
  methods: {

    // 点击物料展示详情
    rowClickInfo(row){
      this.stockinfo2.open = true;
      this.stockinfo2.title = "物料库存信息明细";
      setTimeout(() => {
        this.$refs.stockinfo2.getStockInfoList(row.itemId,row.itemName);
      }, 5);
     /* // this.caiLoading = true
      this.queryParams.itemId=row.itemId
      listinfo(this.queryParams).then(res=>{
        // console.log(res)
        this.stockinfoListInfo = res.rows
      })
      this.infoTitle = ''
      this.infoOpen = true
      this.infoLoading = false*/
    },

    //表格特殊背景色
    tableRowClassName({row, rowIndex}) {
      if(row.kcNum>row.upLine){
        return 'change-row'
      }else if(row.kcNum<row.downLine){
        return 'finance-row'
      }else{
        return ''
      }
      // if(row.financeAuditStat == '已审核'){
      //   // return 'finance-row'
      //   if(row.flagOrderChange>0){
      //     return 'change-row'
      //   }else{
      //     return 'finance-row'
      //   }
      // }else if(row.flagProdCmd == '完成'){
      //   return 'warning-row';
      // }else{
      //   return '';
      // }

    },
    /** 查询库存信息列表 */
    getList() {
      this.loading = true;
      listStockinfo(this.queryParams).then(response => {
        this.stockinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        itemName: null,
        itemId: null,
        lot: null,
        kcNum: null,
        qutlityStauts: null,
        supplyId: null,
        supplyName: null,
        classId: null,
        className: null,
        stockId: null,
        stockName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStockinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStockinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStockinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库存信息编号为"' + ids + '"的数据项？').then(function() {
        return delStockinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/stockinfo/export', {
        ...this.queryParams
      }, `stockinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .el-table .change-row {
    background: #58FAD0;
  }
  ::v-deep .el-table .finance-row{
    background:#F5D0A9;
  }
</style>
